﻿/**************************************************************

项目名称 :ThinkWide数据访问基础库
版权所有 (c) 2010 ThinkWide
+-------------------------------------------------------------
项目: MFDataAdapter数据库服务类


版本: 1


创建日期:2010-5-3


作者: 章称

+-------------------------------------------------------------
描述:z

更新:   2010-5-3

ToDo:

***************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MF.Core;
using System.Reflection;
using MF.Data.SupplyInterface;

namespace MF.Data.ORMapping
{

    /// <summary>
    /// 数据交换适配器
    /// </summary>
    public  class MFDataAdapter
    {
        public IDataService DataService;

        public const string cXMLPath = "ServiceConfig.xml";

        public string SupplyClass { get; set; }
        public string ProviderName { get; set; }
        public string SupplyDllRealPath { get; set; }


        public MFDataServiceConfigurations Configurations;

        private bool Init()
        {
            return this.Init(string.Empty);
        }

        private bool Init(string providerName)
        {

            Configurations = new MFDataServiceConfigurations(MFPath.GetRealPath(cXMLPath));
            ProviderName = providerName;
            try
            {
                MFDataServiceConfiguration Configuration;
                if (providerName != string.Empty)
                    Configuration = Configurations.GetProvider(providerName);
                else
                    Configuration = Configurations.GetDefaultProvider();

                if (Configuration == null) throw new Exception("providerName 默认配置不存在");

                SupplyClass = Configuration.SupplyClass;
                SupplyDllRealPath = MFPath.GetRealPath(Configuration.SupplyDllPath);


                if (SupplyClass == null) throw new Exception("SupplyClass 配置不存在");
                if (SupplyDllRealPath == null) throw new Exception("SupplyDllPath 配置不存在");
                return true;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool ReLoadProvider(string providerName)
        {
            MFDataServiceConfiguration Configuration = Configurations.GetProvider(providerName);
            if (Configuration != null)
            {
                providerName = Configuration.Name;

                SupplyDllRealPath = MFPath.GetRealPath(Configuration.SupplyDllPath);
                SupplyClass = Configuration.SupplyClass;
                
                DataService = this.CreateDataService();
                DataService.Init();
                return true;
            }
            else
                return false;

        }

        public IDataService CreateDataService()
        {

            //string path = SupplyDllRealPath;
            try
            {

                Object obj=null;
                if (true)
                {
                    //Load from self
                    Type t=Type.GetType(SupplyClass);
                    obj = Activator.CreateInstance(t);
                }
                else
                {
                    //Load from file
                    Assembly asm=Assembly.LoadFile(SupplyDllRealPath);
                    obj = asm.CreateInstance(SupplyClass);
                }
                
                
               
                if (obj != null)
                    return (IDataService)obj;
                else
                    return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public MFDataAdapter()
        {
            Init();
            DataService = this.CreateDataService();
            DataService.Init();
        }

        public MFDataAdapter(string provideName)
        {
            Init(provideName);
            DataService = this.CreateDataService();
            DataService.Init();
        }

        public DataSet GetDataSet(string sql)
        {
            return DataService.GetDataSet(sql);
        }

        public int SaveDataTable(DataTable dataTable)
        {
            return DataService.SaveDataTable(dataTable);
        }

        public int ExecuteSQL(string sql)
        {
            return DataService.ExecuteSQL(sql);
        }

        public MFDBResuleDataSetInfo ExecuteSQLDataSet(string sql, CommandType commandType, MFDataParameters dataParameters, int commandTimeOut)
        {
            return DataService.ExecuteSQLDataSet(sql, commandType, dataParameters, commandTimeOut);
        }

        public MFDBResuleInfo ExecuteSQL(string sql, CommandType commandType, MFDataParameters dataParameters, int commandTimeOut)
        {
            return DataService.ExecuteSQL(sql, commandType, dataParameters, commandTimeOut);
        }

        
    }
}
